﻿using parkMonitor.entity;
using parkMonitor.model;
using parkMonitor.viewModel.objectTree;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace parkMonitor.server.monitorServer {
	/// <summary>激光监控</summary>
	public class LaserMonitorCtl: IMonitorCtl {
		MonitorModel monitorMd = null;

		ListMiddleParam laserMd = null;

		public LaserMonitorCtl(MonitorModel _monitorMd) {
			monitorMd = _monitorMd;
		}

		public void init() {
			initMd();
		}

		public void getData() {
			IEquipments laser = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.Laser);
			if(laser == null) {
				return;
			}

			LaserMessage data = laser.GetMessage() as LaserMessage;
			if(data == null) {
				return;
			}

			setMd(data);
		}

		private void setMd(LaserMessage src) {
			laserMd[0].value = src.id.ToString();
			laserMd[1].value = src.status.ToString();

			laserMd[2].value = src.data.centerX.ToString();
			laserMd[3].value = src.data.centerY.ToString();
			laserMd[4].value = src.data.angleA.ToString();
			laserMd[5].value = src.data.length.ToString();
			laserMd[6].value = src.data.width.ToString();
			laserMd[7].value = src.data.height.ToString();
		}

		/// <summary>初始化激光状态</summary>
		private void initMd() {
			//mapNumMachineMd
			ListMiddleParam md = new ListMiddleParam() {
				type = DeviceType.Laser,
				imgType = DeviceType.Laser,
				uiType = DevTreeItemShowType.ImageList,
				nameDesc = "激光状态",
				shortNameType = "激光状态"
			};
			md.Add(createParamMd("ID", ""));
			md.Add(createParamMd("状态", "4", getStatusCbx()));
			md.Add(createParamMd("中心X", ""));
			md.Add(createParamMd("中心Y", ""));
			md.Add(createParamMd("角度", ""));
			md.Add(createParamMd("长度", ""));
			md.Add(createParamMd("宽度", ""));
			md.Add(createParamMd("高度", ""));

			laserMd = md;

			var treeVM = monitorMd.getTreeRootVM(DeviceType.Laser);
			if(treeVM != null) {
				MainModel.ins.mainWin.Dispatcher.Invoke(() => {
					monitorMd.initMiddleMdToVM(treeVM, md, "激光雷达&激光状态");
				});
			}
		}

		private Dictionary<string, CbxModel> getStatusCbx() {
			Dictionary<string, CbxModel> cbx = new Dictionary<string, CbxModel>() {
				{"0", new CbxModel() { desc= "就绪"		,shortDesc="就绪"		, value="0", warnLevel = AlarmStatus.Normal } },
				{"1", new CbxModel() { desc= "开始测量"	,shortDesc="开始测量"	, value="1", warnLevel = AlarmStatus.Normal } },
				{"2", new CbxModel() { desc= "工作中"	,shortDesc="工作中"        , value="2", warnLevel = AlarmStatus.Normal } },
				{"3", new CbxModel() { desc= "测量完成"	,shortDesc="测量完成"	, value="3", warnLevel = AlarmStatus.Normal } },
				{"4", new CbxModel() { desc= "结算异常"	,shortDesc="结算异常"	, value="4", warnLevel = AlarmStatus.Error } },
				{"5", new CbxModel() { desc= "系统异常"	,shortDesc="系统异常"	, value="5", warnLevel = AlarmStatus.Error } },
			};

			return cbx;
		}

		private DevParamModel createParamMd(string name, string value, Dictionary<string, CbxModel> cbx = null) {
			DevParamModel md = new DevParamModel() {
				addr = MonitorModel.lasserAddress,
				typeDesc = "激光雷达",
				subTypeDesc = "激光状态",
				statusDesc = name,
				value = value
			};
			if(cbx != null) {
				md.showType = ParamShowType.e_cbx;
				md.mapCbx = cbx;
			}
			return md;
		}

		public void clear() {

		}

	}
}
